/**
 * Created by yujianfu on 2019/9/11.
 */
import React from 'react'

import {Table, Form, DatePicker, Button, Select, Radio} from 'antd'
const {RangePicker} = DatePicker
const FormItem = Form.Item
const {Option} = Select;
import {fetchPost} from '../../utils/request'
import moment from 'moment'
import ReportCommon from './ReportCommon'

class Common extends React.Component {

    constructor(props) {
        super(props)
        this.state = {
            params: {
                loanType: "1",
                startDate: moment(moment().subtract(7, 'days'), 'YYYY-MM-DD'),
                endDate: moment(moment(), 'YYYY-MM-DD'),
                allFoundCodes: null
            },
            fundList: [],
            startDate: null,
            endDate: null,
            loanType: 1,
            statsKeysArray: null
        }
    }


    componentDidMount() {
        console.log(this.props.params)
        let statsKeys = null
        if (this.props.params != null && this.props.params.statsKey != null) {
            statsKeys = this.props.params.statsKey
        } else {
            statsKeys = this.props.statsKey
        }

        var statsKeysArray = statsKeys.split("|");


        this.setState({
            ...this.state,
            statsKeysArray: statsKeysArray,
        }, () => {
            console.log(this.state.statsKeysArray);
        })
    }


    handleSubmit = (e) => {
        e.preventDefault()
        this.props.form.validateFields((err, fieldsValue) => {
            const rangeValue = fieldsValue['range-picker'];
            const loanType = fieldsValue['loanType'];
            let allFoundCodes = fieldsValue['fundCode'];
            let startDate = moment(rangeValue[0], 'YYYY-MM-DD');
            let endDate = moment(rangeValue[1], 'YYYY-MM-DD');

            if(allFoundCodes==null){
                allFoundCodes = this.state.params.allFoundCodes
            }
            this.setState({
                params: {startDate, endDate, loanType, allFoundCodes}
            })
        })
    }


    render() {

        if (this.state.statsKeysArray == null) {
            return <div></div>
        }
        const dateFormat = 'YYYY-MM-DD'
        const rangeConfig = {
            rules: [{type: 'array', required: true, message: 'Please select Time!'}],
            initialValue: [this.state.startDate != null ? moment(this.state.startDate, dateFormat) : moment().subtract(7, 'days'),
                this.state.endDate != null ? moment(this.state.endDate, dateFormat) : moment()]
        }

        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 8},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 16},
            },
        }
        const {getFieldDecorator} = this.props.form;

        console.log(6666, this.state.statsKey);
        return (
            <div>
                <Form layout="inline" onSubmit={this.handleSubmit}>
                    <FormItem
                        {...formItemLayout}
                        label="ApplyDate"

                    >
                        {getFieldDecorator('range-picker', rangeConfig)(
                            <RangePicker/>
                        )}
                    </FormItem>

                    <FormItem
                        label="LoanType"
                    >
                        {getFieldDecorator('loanType', {initialValue: "1"})(
                            <Radio.Group >
                                <Radio.Button value="0,1">All</Radio.Button>
                                <Radio.Button value="1">New</Radio.Button>
                                <Radio.Button value="0">Repeat</Radio.Button>
                            </Radio.Group>
                        )}
                    </FormItem>

                    <FormItem>
                        <Button type="primary" htmlType="submit">Search</Button>
                    </FormItem>

                </Form>

                {this.state.statsKeysArray.map((k, index) => {
                    return <div>
                        <br/>
                        <br/>
                        <ReportCommon key={k} params={this.state.params} queryKey={k} name={k}/>
                        <br/>
                    </div>
                })}
                <br/>
                <br/>

                <br/>
                <br/>
            </div>
        )
    }
}
const a = Form.create()(Common)
export default a

